#include "chessboard.h"
#include "ui_chessboard.h"

ChessBoard::ChessBoard(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ChessBoard)
{
    ui->setupUi(this);

    connect(this,&ChessBoard::signal_clearBoard,[=]()   //用户重新选择模式,棋盘清空
    {
        vec.clear();
    });
    connect(this,&ChessBoard::signal_whiteTimeout,[=]() //白棋倒计时结束
    {
        QMessageBox::information(0,"消息","白棋超时,黑棋胜利!",QMessageBox::Ok);
        vec.clear();
        emit signal_gameover(); //游戏结束信号
        ChessColor = White; //新一局游戏依旧白子先走
        update();
        return;
    });
    connect(this,&ChessBoard::signal_blackTimeout,[=]() //黑棋倒计时结束
    {
        QMessageBox::information(0,"消息","黑棋超时,白棋胜利!",QMessageBox::Ok);
        vec.clear();
        emit signal_gameover(); //游戏结束信号
        ChessColor = White; //新一局游戏依旧白子先走
        update();
        return;
    });
    connect(this,&ChessBoard::signal_GameMode,[=](int type) //确定用户选择的模式,以此绘制相应的界面
    {
       gamemode = type;
    });

    connect(this,&ChessBoard::signal_recevePeerChess,[=](int color,QPoint pos)//网络对战接收对方落子信息
    {
        Chess chess(pos,color);
        vec.push_back(chess);
        update();
    });

    connect(this,&ChessBoard::signal_receveWin,[=](int color)   //网络对战接收某一方获胜消息
    {
        QString str = color==White?"白棋胜利!":"黑旗胜利!";
        QMessageBox::information(0,"消息",str,QMessageBox::Ok);
        vec.clear();
        update();
        return;
    });
}

ChessBoard::~ChessBoard()
{
    delete ui;
}

int ChessBoard::detectionChess(Chess chess,QPoint pos)
{
    int count = 0;
    chess.pos += pos;
    while(vec.contains(chess))  //是否存在该棋子
    {
        count++;
        chess.pos += pos;
    }
    return count;
}

void ChessBoard::paintEvent(QPaintEvent *evnet)
{

    QPainter painter(this);
    painter.setPen(QPen(Qt::black,2));
    painter.setRenderHints(QPainter::Antialiasing);
    //painter.drawImage(QPoint(0,0),QImage(":/new/src/chessboard.png"));
    //画背景
    painter.drawImage(QPoint(0,0),QImage(":/new/src/back.jpg"));
    //画棋盘
    for(int i = 0;i<Board_Rows_Cols;i++)
    {
        //竖线
        QPoint p1 = QPoint(Board_Offset_Window+i*Board_Cell_WH,Board_Offset_Window);
        QPoint p2 = QPoint(Board_Offset_Window+i*Board_Cell_WH,this->height()-Board_Offset_Window);
                painter.drawLine(p1,p2);
        //横线
               p1 = QPoint(Board_Offset_Window,Board_Offset_Window+Board_Cell_WH*i);
               p2 = QPoint(this->width()-Board_Offset_Window,Board_Offset_Window+Board_Cell_WH*i);
               painter.drawLine(p1,p2);
    }
    //画四个点
    painter.setBrush(QBrush(Qt::black));
    painter.drawEllipse(QPoint(175,175),10,10);
    painter.drawEllipse(QPoint(575,175),10,10);
    painter.drawEllipse(QPoint(175,575),10,10);
    painter.drawEllipse(QPoint(575,575),10,10);
    //画棋子
    for(int i = 0;i<vec.size();i++)
    {
        switch(vec[i].color)
        {
        case White:painter.drawImage(vec[i].pos,QImage(":/new/src/whitechess.png"));
            break;
        case Black:painter.drawImage(vec[i].pos,QImage(":/new/src/blackchess.png"));
            break;
        }
    }

    switch(ChessColor)
    {
    case White:
        setCursor(QCursor(QPixmap(":/new/src/whitechess.png")));
        break;
    case Black:
         setCursor(QCursor(QPixmap(":/new/src/blackchess.png")));
        break;
    }
}

void ChessBoard::mousePressEvent(QMouseEvent *ev)
{
    if(gamemode == 2)   //双人对战
    {
        if(ev->button() == Qt::LeftButton)//左键按下才落子
        {
            QPoint pos = ev->pos();
            pos = QPoint((pos.x()/Board_Cell_WH)*Board_Cell_WH,
                         (pos.y()/Board_Cell_WH)*Board_Cell_WH);  //棋子图片中心坐标,保证棋子落子位置正确
            Chess chess(pos,ChessColor);
            //如果落子处已经有其他子,则不允许落子
            for(int i = 0;i<vec.size();i++)
            {
                if(vec[i].pos == pos)
                return;
            }
            vec.push_back(chess);       //落下的子加入vector进行管理
            //如果落子处存在胜利情况(五子连珠),则胜利
            //判断8个方向的连子情况
            int left        = detectionChess(chess,QPoint(-1*Board_Cell_WH,0));
            int right       = detectionChess(chess,QPoint(1*Board_Cell_WH,0));
            int up          = detectionChess(chess,QPoint(0,-1*Board_Cell_WH));
            int down        = detectionChess(chess,QPoint(0,1*Board_Cell_WH));
            int leftup      = detectionChess(chess,QPoint(-1*Board_Cell_WH,-1*Board_Cell_WH));
            int rightup     = detectionChess(chess,QPoint(1*Board_Cell_WH,-1*Board_Cell_WH));
            int leftdown    = detectionChess(chess,QPoint(-1*Board_Cell_WH,1*Board_Cell_WH));
            int rightdown   = detectionChess(chess,QPoint(1*Board_Cell_WH,1*Board_Cell_WH));


            //获胜
            if((left+right) >= 4 || (up + down) >= 4 || (leftup+rightdown) >= 4 || (rightup + leftdown) >= 4)
            {

                QString str = ChessColor==White?"白棋胜利!":"黑旗胜利!";
                QMessageBox::information(0,"消息",str,QMessageBox::Ok);
                vec.clear();
                emit signal_gameover(); //游戏结束信号
                ChessColor = White; //新一局游戏依旧白子先走
                update();
                return;
            }

            switch(ChessColor)
            {
            case White:
                ChessColor = Black;break;
            case Black:
                ChessColor = White;break;
            }
            emit signal_timerChange();
        }
        update();
    }
    else if(gamemode == 1)  //网络对战
    {
        if(ev->button() == Qt::LeftButton)//左键按下才落子
        {
            QPoint pos = ev->pos();
            pos = QPoint((pos.x()/Board_Cell_WH)*Board_Cell_WH,
                         (pos.y()/Board_Cell_WH)*Board_Cell_WH);  //棋子图片中心坐标,保证棋子落子位置正确
            Chess chess(pos,ChessColor);
            //如果落子处已经有其他子,则不允许落子
            for(int i = 0;i<vec.size();i++)
            {
                if(vec[i].pos == pos)
                return;
            }
            emit signal_chesspos(ChessColor,pos);
            vec.push_back(chess);       //落下的子加入vector进行管理
            //如果落子处存在胜利情况(五子连珠),则胜利
            //判断8个方向的连子情况
            int left        = detectionChess(chess,QPoint(-1*Board_Cell_WH,0));
            int right       = detectionChess(chess,QPoint(1*Board_Cell_WH,0));
            int up          = detectionChess(chess,QPoint(0,-1*Board_Cell_WH));
            int down        = detectionChess(chess,QPoint(0,1*Board_Cell_WH));
            int leftup      = detectionChess(chess,QPoint(-1*Board_Cell_WH,-1*Board_Cell_WH));
            int rightup     = detectionChess(chess,QPoint(1*Board_Cell_WH,-1*Board_Cell_WH));
            int leftdown    = detectionChess(chess,QPoint(-1*Board_Cell_WH,1*Board_Cell_WH));
            int rightdown   = detectionChess(chess,QPoint(1*Board_Cell_WH,1*Board_Cell_WH));


            //获胜
            if((left+right) >= 4 || (up + down) >= 4 || (leftup+rightdown) >= 4 || (rightup + leftdown) >= 4)
            {
                emit signal_win(ChessColor);
                QString str = ChessColor==White?"白棋胜利!":"黑旗胜利!";
                QMessageBox::information(0,"消息",str,QMessageBox::Ok);
                vec.clear();
                update();
                return;
            }
            update();
        }
    }
    else if(gamemode == 0)  //人机对战
    {

    }

}
